import { AppStore } from './../../services/appStore';
import { SysSvr } from './../../services/system';
import { Terminal } from './../../models/terminal';
import { StoreSvr } from './../../services/store';
import { Http } from '@angular/http';
import { LoadMask } from './../../components/loadmask';
import { User } from './viewmodel';
import { NavigateService } from './../../services/navigate';
import { Authentication } from './../../services/authtication';
import { MessageBoxService, GrowlMessageType } from './../../components/messageBox';
import { Component, Directive, ElementRef, OnInit } from '@angular/core';
import { loadingMask } from './../../components/loadmask';


@Component({
    selector: 'bind-device',
    styleUrls: ['./initialization.scss'],
    template: `
        <div class="binddevice-container">
            <header>
                <i class="material-icons back divvma" (click)="onBack()">arrow_back</i>
                <h1 class="padding">选择厨显方案</h1>
                <div class="nav">
                    <i ></i>
                    <i ></i>
                    <i ></i>
                    <i ></i>
                    <i class="active">5</i>
                </div>
            </header>
            <div class="margin terminal">
                <h3 class="title">设置终端名称</h3>
                <input type="text" placeholder="终端名称" [(ngModel)]="deviceName"/>
                <p class="content">终端名称不能超过10个字符</p>
            </div>
            <div class="margin solution" *ngIf="kitchens">
                <h3 class="title">选择厨房方案</h3>
                <select class="form-control" [ngModel]="kitchen" (ngModelChange)=change($event)>
                    <option *ngFor="let kitchen of kitchens" [value]="kitchen.id">{{kitchen.name}}</option>
                </select>
                <p class="description">出品台支持多档口出品，在不同的档口选择合适的厨房显示方案。厨房显示方案在店铺管理"厨房打印显示方案设置"中设置</p>
            </div>
            <button class="margin" (click)="onStart()">开始使用</button>
        </div>
    `,
    providers: []
})

export class BindDevice implements OnInit {
    constructor(
        private navSvr: NavigateService,
        private storeSvr: StoreSvr,
        private appStore: AppStore
    ) {

    }
    private terminal: Terminal = <any>{};
    private deviceName;
    private kitchens: Array<any>;
    private currentKitchen: any;
    private kitchen: any = "all";
    private store = {};

    @loadingMask()
    async onStart() {
        if (!this.deviceName) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, "", "请输入终端名称！");

            return;
        }

        if (this.deviceName.length > 10) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, "", "终端名称不能超过10个字符");

            return;
        }

        try {
            const k = this.kitchens.find(x => x.id == this.kitchen);
            this.storeSvr.currentKitchenSolutions = [k];
            //没有终端,先初始化一次
            this.storeSvr.Terminal = { id: "expetiterTerminal" }//await this.storeSvr.BindTerminal(this.deviceName);
            let result = (await this.storeSvr.bindKitchenDevice(k.id, this.deviceName));
            //第一次登录在这里触发持续监测
            this.appStore.systemStore.actions.doBackgroundTask();
            this.appStore.systemStore.systemObservable.subscribe(system => {
                system.nodeServices && system.nodeServices.forEach(x => {
                    if (x.status == "failed") {
                        MessageBoxService.current.nodeServiceWarn(x);
                    } else {
                        MessageBoxService.current.nodeServiceWarn(null);
                    }
                })
            });
            (<any>window).device && (<any>window).device.fullScreen(true) //debug
            location.reload();
        } catch (error) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, "绑定终端出错", error.message || error);
        }
    }

    onBack() {
        this.navSvr.navigate(["initialization/checkMode"]);
    }
    change(id) {
        this.kitchen = id;
        this.currentKitchen = this.kitchens.find(x => x.id == id);
    }
    async ngOnInit() {
        //todo 一直坚持ldc
        try {
            this.store = this.storeSvr.CurrentStore;
            this.deviceName = (await this.storeSvr.getTerminalName()).terminalName;
            const result = await this.storeSvr.getKdsKitchens();
            this.kitchens = result;
        } catch (error) {

        }
    }
}
